﻿using System.Collections.Generic;
using FibMatrix.Task;

namespace FibMatrix
{
	[FM_Mono.IgnoreGen]
	public class LoadBundleFromDiskPipe : IResourceLoadPipe
	{
		public FMTask Handle(IResourcePipelineContext rawContext, out ResourceError error)
		{
			error = ResourceError.None;
			ResourcePipelineContext context = rawContext as ResourcePipelineContext;
			AssetBundleLoadItem mainHint = context.item as AssetBundleLoadItem;
			List<BundleInfo> dependence = ResourceHelper.GetFixedFlatDependences(mainHint.bundleInfo);

			for (int i = dependence.Count - 1; i >= 0; --i)
			{
				BundleInfo hint = dependence[i];


				if (hint.bundleHandle == null)
				{
					hint.bundleHandle = BundleLoadManager.instance.LoadBundle(hint.url, hint.md5/*, hint.id*/);
				}
				if (hint.bundleHandle == null || hint.bundleHandle.bundle == null)
				{
					error = ResourceError.FileNotFound;
					break;
				}
			}
		
			return null;
		}
	}
}
